﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CSM.Controllers;
using CSM.Models;
using System.Text;

namespace CSM.Views.Wenjuan
{
    public partial class MakeWenjuan : System.Web.UI.Page
    {
        CustomDataContext db = new CustomDataContext();
        public string alert;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindMyData(0, "");
                int Totals = TotalPageAmount("");
                int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);
                Label_Total.Text = "共" + pages.ToString();
                Label_CurrenPageAndTo.Text = "页，当前第1" + " 页，转到";

            }
        }
        #region 绑定数据及翻页
        private void BindMyData(int PageIndex, string starts)
        {
            int skipAccount = PageIndex * 10;
            var MyQuery = (from a in db.TCrm_WJ_Statistics
                           where a.StatName.StartsWith(starts)
                           select new { 
                               a.StatID,
                               a.StatName,
                               a.CreateTime,
                               a.UserID,
                               ItemID = (from c in db.TCrm_Sys_Items where c.ClassID == "wjlx" && c.DefaultValue == a.StatID select c).First().ItemID == null ? "0" : (from c in db.TCrm_Sys_Items where c.ClassID == "wjlx" && c.DefaultValue == a.StatID select c).First().ItemID,
                           }).Skip(skipAccount).Take(10);
            GridView_Ques.DataSource = MyQuery;
            GridView_Ques.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount(string starts)
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TCrm_WJ_Statistics
                       where a.StatName.StartsWith(starts)
                       select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            string starts = txtUser.Text;
            int Totals = TotalPageAmount(starts);
            int pages = (Totals % 10) == 0 ? (Totals / 10) : (Totals / 10 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        if (TextBox_PageIndex.Text == "")
                        { pageIndx = 0; }
                        else
                        {
                            int xx = Convert.ToInt32(TextBox_PageIndex.Text);
                            if (xx > pages)
                            { pageIndx = pages - 1; }

                            else
                            { pageIndx = xx - 1; }


                        }
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx, starts);

        }

        #endregion
        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            var query1 = from a in db.TCrm_Sys_Items where a.DefaultValue == myid select a;
            if (query1.Count() == 0)
            {
                    var query2 = from a in db.TCrm_WJ_CustomerAnswer where a.StatID == myid select a;
                    if (query2.Count() == 0)
                    {
                        Response.Redirect("DoQues.aspx?id=" + myid);
                    }
                    else
                    {
                        alert = "<script>alert('当前问卷已有客户回答，不能编辑');</script>";
                    }
            }
            else
            {
                alert = "<script>alert('当前问卷为默认问卷，不能编辑');</script>";
            }
            
        }
        protected void Button_Default_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            foreach (GridViewRow gvr in GridView_Ques.Rows)
            {
                DropDownList x = (DropDownList)gvr.FindControl("DropDownList_Level");
                x.Enabled = false;
            }
            Button ib = (Button)sender;
            DropDownList ddl = (DropDownList)ib.Parent.Parent.FindControl("DropDownList_Level");
            ddl.Enabled = true;
            DefaultID.Value = myid;
        }
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect("DoQues.aspx");
        }

        protected void Button_Delete_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            var query = from a in db.TCrm_Sys_Items where a.DefaultValue == myid select a;
            if (query.Count() == 0)
            {
                var query2 = from a in db.TCrm_WJ_CustomerAnswer where a.StatID == myid select a;
                if (query2.Count() == 0)
                {
                    var MyQueryFirst = from a in db.TCrm_WJ_Statistics
                                  where a.StatID == myid
                                  select a;
                    var MyQuerySecond = from a in db.TCrm_WJ_Statistics_Detail where a.StatID == myid select a;
                    //以前保存的数据删除
                        db.TCrm_WJ_Statistics.DeleteAllOnSubmit(MyQueryFirst);
                        db.SubmitChanges();
                        db.TCrm_WJ_Statistics_Detail.DeleteAllOnSubmit(MyQuerySecond);
                        db.SubmitChanges();
                        alert = "<script>alert('删除成功');</script>";
                        BindMyData(0, "");
                }
                else
                {
                    alert = "<script>alert('该问卷存在用户回答，请先删除该问卷的用户回答记录');</script>";
                }
            }
            else
            {
                alert = "<script>alert('该试卷为默认试卷，不能删除');</script>";
            }

        }
        protected void Button_Browse_Click(object sender, EventArgs e)
        {
            string user = txtUser.Text;
            BindMyData(0, user);
        }


        protected void DropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            string selectid = ddl.SelectedValue;
            if (selectid == "0")
            {
                var query = from a in db.TCrm_Sys_Items where a.DefaultValue == DefaultID.Value select a;
                if (query.Count() == 1)
                {
                    query.Single().DefaultValue = null;
                    db.SubmitChanges();
                }
                else
                {
                    return;
                }
            }
            else{
            var query = from a in db.TCrm_Sys_Items where a.ItemID == selectid select a;
            query.Single().DefaultValue = DefaultID.Value;
            db.SubmitChanges();
            }
            BindMyData(0, "");
        }
    }
}
